package demoMod.invincibleOne.cards.invincible;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardQueueItem;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.localization.CardStrings;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.actions.SelectCardInCardGroupAction;
import demoMod.invincibleOne.enums.CardTagsEnum;

import java.util.List;
import java.util.stream.Collectors;

public class BloodReflux extends AbstractInvincibleCard {
    public static final String ID = InvincibleOneMod.makeID("BloodReflux");

    private static final CardStrings cardStrings = CardCrawlGame.languagePack.getCardStrings(ID);
    public static final String NAME = cardStrings.NAME;
    public static final String DESCRIPTION = cardStrings.DESCRIPTION;
    public static final String IMG_PATH = "cards/BloodReflux.png";

    private static final CardType TYPE = CardType.SKILL;
    private static final CardRarity RARITY = CardRarity.RARE;
    private static final CardTarget TARGET = CardTarget.NONE;

    private static final int COST = -2;

    public BloodReflux() {
        super(ID, NAME, InvincibleOneMod.getResourcePath(IMG_PATH), COST, DESCRIPTION, TYPE, RARITY, TARGET);
        this.tags.add(CardTagsEnum.VIRUS);
        this.baseMagicNumber = this.magicNumber = 1;
    }

    @Override
    public Runnable getUpgradeAction() {
        return () -> {
            upgradeMagicNumber(100);
            this.rawDescription = cardStrings.UPGRADE_DESCRIPTION;
            this.initializeDescription();
        };
    }

    @Override
    public void triggerOnEndOfTurnForPlayingCard() {
        this.dontTriggerOnUseCard = true;
        addToBot(new AbstractGameAction() {
            @Override
            public void update() {
                AbstractDungeon.actionManager.cardQueue.add(0, new CardQueueItem(BloodReflux.this, true));
                isDone = true;
            }
        });
    }

    @Override
    public void use(AbstractPlayer p, AbstractMonster m) {
        if (this.dontTriggerOnUseCard) {
//            if (upgraded) {
//                addToBot(new SelectCardInCardGroupAction(this.magicNumber, c -> (c.type == CardType.CURSE || c.hasTag(CardTagsEnum.VIRUS)) && !c.cardID.equals(ID), c -> p.hand.moveToDeck(c, true), p.hand));
//            return;
//            }
            List<AbstractCard> cards = p.hand.group.stream().filter(c -> (c.type == CardType.CURSE || c.hasTag(CardTagsEnum.VIRUS)) && !(c instanceof BloodReflux)).collect(Collectors.toList());
            int amount = this.magicNumber;
            while (!cards.isEmpty() && amount > 0) {
                AbstractCard card = cards.remove(AbstractDungeon.cardRandomRng.random(cards.size() - 1));
                p.hand.moveToDeck(card, true);
                amount--;
            }
        }
    }
}
